﻿using ApplicationModels.A07.Chats;
using Commons.Requests;
using Commons.Responses;
using EntityModels.E07.Chats;
using IApplicationServices;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    public class ConversationController:BaseController<int, Conversation, ConversationAM>
    {
        private readonly IConversationService _conversationService;
        public ConversationController(IConversationService service):base(service)
        {
            _conversationService = service;
        }
        /// <summary>
        /// 隐藏会话
        /// </summary>
        /// <param name="receiverId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageResponse> HideAsync(string? receiverId)
        {
            var res = (await ApiService.GetAsync(x => x.ParticipantIds.Contains(AppUserId) && x.ParticipantIds.Contains(receiverId))).Data;
            if(res == null)
            {
                return new MessageResponse("会话不存在", false);
            }
            res.DisplayIds.Remove(AppUserId);
            return await ApiService.UpdateAsync(res);
        }
        public override async  Task<MessageResponse> AddAsync(ConversationAM apiEntity)
        {
            apiEntity.SenderId = AppUserId;
            return await _conversationService.AddAsync(apiEntity);
        }
        /// <summary>
        /// 获取会话列表
        /// </summary>
        /// <param name="pagingRequest"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<DatasResponse<ConversationAM>> GetConversationsAsync()
        {
            return await _conversationService.GetConversations(AppUserId);
        }
    }
}
